package com.org.salesforce;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.SeekBar;

public class Http_Url_Class extends AsyncTask<String, Void, String>{

	private HttpURLConnection http_url_conn;
	
	String resourceData;
	
	@Override
	protected String doInBackground(String... params) {
		try{
		URL w_Url = new URL(params[0]);
		http_url_conn = (HttpURLConnection) w_Url.openConnection();
		
		String arguments = params[1];
		if(arguments.equalsIgnoreCase("Login")){
			getLogin();
		}
		else{
			retriveData();
		}
		}
		catch(Exception ex)
		{
			
		}

		return resourceData;                                                                        
	}
	
	public void getLogin(){
		try {
		http_url_conn.setRequestMethod("POST");
		http_url_conn.setRequestProperty("Content-type", "application/json");
		
		
			Log.i("NCS","Response code is  :"+http_url_conn.getResponseCode());
			Log.i("NCS","Response Method is  :"+http_url_conn.getRequestMethod());
			Log.i("NCS","Response Message is  :"+http_url_conn.getResponseMessage());
			
			resourceData = convert_To_String(http_url_conn.getInputStream());
				JSONObject jsonObject = new JSONObject(resourceData);
				
				Session.sInstanceUrl = jsonObject.getString("instance_url");
				Session.sAccessToken = jsonObject.getString("access_token");
				
				Log.i("NCS","InstanceUrl"+Session.sInstanceUrl);
				
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public void retriveData(){
		try {
			Log.i("NCS","AccessToken"+Session.sAccessToken);
		http_url_conn.setRequestMethod("GET");
		http_url_conn.setRequestProperty("Authorization", "OAuth "+Session.sAccessToken);
		
			Log.i("RGT","Response code is  :"+http_url_conn.getResponseCode());
			Log.i("RGT","Response Method is  :"+http_url_conn.getRequestMethod());
			Log.i("RGT","Response Message is  :"+http_url_conn.getResponseMessage());
			
			resourceData = convert_To_String(http_url_conn.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
		}
		
	}
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		
		MainActvity main_Activity = new MainActvity();
		//main_Activity.parsing_Response(result);
	}
	
	public static String convert_To_String(InputStream is)
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
		while ((line = reader.readLine()) != null) {
		
		sb.append(line + "\n");
		Log.i("TPT","Line is  :"+ line);
		}
		} catch (IOException e) {
		e.printStackTrace();
		} finally {
		try {
		is.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		return sb.toString();
		}

}
